package com.smugmugsoftware.airconcalc.advanced;

import java.util.Iterator;
import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class RoomListAdapter<T> extends ArrayAdapter<Room> {

	Context context;
	Vector<Room> data;

	public RoomListAdapter(Context context,Vector<Room> data) {
		super(context, android.R.layout.simple_list_item_1, data);
		this.context = context;
		this.data = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		RowHolder holder = null;

		if(row == null) {
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			row = inflater.inflate(R.layout.room_list_item, parent, false);

			holder = new RowHolder();
//			holder.id = (TextView) row.findViewById(R.id.ID);
			holder.idLabel = (TextView) row.findViewById(R.id.IDLabel);
			holder.walls = (TextView) row.findViewById(R.id.walls);
			holder.windows = (TextView) row.findViewById(R.id.windows);
			holder.doors = (TextView) row.findViewById(R.id.doors);
			holder.ceilings = (TextView) row.findViewById(R.id.ceilings);
			holder.floors = (TextView) row.findViewById(R.id.floors);
			holder.sensibleLoads = (TextView) row.findViewById(R.id.sensible);
			holder.roomLoad = (TextView) row.findViewById(R.id.room_load);

			row.setTag(holder);
		} else {
			holder = (RowHolder) row.getTag();
		}

		Room thisRoom = data.get(position);

//		holder.id.setText(String.valueOf(thisRoom.getId()));
		holder.idLabel.setText(thisRoom.getName());
		holder.walls.setText(String.valueOf(thisRoom.getWalls().size()));
		holder.sensibleLoads.setText(String.valueOf(thisRoom.getSensibleLoads().size()));
		holder.roomLoad.setText(thisRoom.getBTUString());
		
//		holder.roomLoad.setText(String.valueOf((int) thisRoom.getTotalRoomLoad(context)));
		
		int countYes = 0;
		int countNo = 0;
		Iterator<CeilingFloor> ceilingIterator = thisRoom.getCeilingsFloors().iterator();
		while (ceilingIterator.hasNext()) {
			if (ceilingIterator.next().isACeiling()) {
				countYes++;
			} else {
				countNo++;
			}
		}
		holder.ceilings.setText(String.valueOf(countYes));
		holder.floors.setText(String.valueOf(countNo));

		countYes = 0;
		countNo = 0;
		Iterator<WindowDoor> windowIterator = thisRoom.getWindowsDoors().iterator();
		while (windowIterator.hasNext()) {
			if (windowIterator.next().isAWindow()) {
				countYes++;
			} else {
				countNo++;
			}
		}
		holder.windows.setText(String.valueOf(countYes));
		holder.doors.setText(String.valueOf(countNo));

		return row;
	}

	static class RowHolder
	{
//		public TextView id;
		public TextView idLabel;
		public TextView walls;
		public TextView windows;
		public TextView doors;
		public TextView ceilings;
		public TextView floors;
		public TextView sensibleLoads;
		public TextView roomLoad;
	}
}
